package com.zhangye.test.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ArComment;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.service.AdminService;
import com.zhangye.test.service.ArCommentService;
import com.zhangye.test.service.EasyArService;
import com.zhangye.test.service.SafeSessionService;

/**
 * 添加作品
 * 作者： 张叶
 * 创建时间：2018年7月20日  下午2:31:08
 */
@Controller
@RequestMapping("api/arComment")
public class ArCommentApi {

	@Autowired
	private ArCommentService arCommentService;
	@Autowired
	private SafeSessionService safeSessionService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private EasyArService easyArService;
	

	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年7月20日  下午2:52:47
	 */
	@ResponseBody
	@RequestMapping(value = "/insert",method = RequestMethod.POST)
	public ApiResult insert(Long commentId,int easyArId,String content,HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(content != null && !"".equals(content) && easyArId != 0){
				ArComment ac = new ArComment();
				ac.setEasyArId(easyArId);
				if(commentId != null){
					ArComment acParent = arCommentService.selectById(commentId);
					if(acParent != null && !"".equals(acParent)){
						if(acParent.getParentId() != 0){
							ac.setParentId(acParent.getParentId());	
						}else{
							ac.setParentId(acParent.getId());
						}
						ac.setDepth(acParent.getDepth() + 1);	
					}else{
						ac.setParentId(0L);
						ac.setDepth(1);
					}
				}else{
					ac.setParentId(0L);
					ac.setDepth(1);
				}
				ac.setContent(content);
				ac.setAdminId(admin.getId());
				ac.setCreatedAt(new Date());
				ac.setCounts(0);
				int i = arCommentService.insert(ac);
				if(i > 0){
					result.msg.status = 0;
					result.msg.desc = "评论成功";
				}else {
					result.msg.status = 1;
					result.msg.desc = "系统错误，评论失败";
				}
			} else {
				result.msg.status = 3;
				result.msg.desc = "参数不能为空";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	
	/**
	 * 点赞评论
	 * 作者： 张叶
	 * 创建时间：2018年7月21日  上午10:45:12
	 */
	@ResponseBody
	@RequestMapping(value = "/give",method = RequestMethod.GET)
	public ApiResult give(Long commentId,HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(commentId != null && commentId != 0){
				ArComment ac = arCommentService.selectById(commentId);
				if(ac != null && !"".equals(ac)){
					//判断是否已经点过
					String caId = ac.getCountAdminId();
					if(caId != null && !"".equals(caId)){
						if(caId.indexOf("," + admin.getId() + ",") != -1){
							ac.setCounts(ac.getCounts() - 1);
							ac.setCountAdminId(caId.replace(admin.getId() + ",", ""));
						}else{
							ac.setCounts(ac.getCounts() + 1);
							ac.setCountAdminId(caId + admin.getId() + ",");
						}
					}else{
						ac.setCounts(ac.getCounts() + 1);
						ac.setCountAdminId("," + admin.getId() + ",");
					}
					int i = arCommentService.updateByPrimaryKey(ac);
					if(i > 0){
						result.msg.status = 0;
						result.msg.desc = "操作成功";
					}else{
						result.msg.status = 1;
						result.msg.desc = "系统错误，操作失败";
					}
				}else{
					result.msg.status = 0;
					result.msg.desc = "操作成功";
				}
			}else{
				result.msg.status = 3;
				result.msg.desc = "参数不能为空";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	
	
	/**
	 * 获取评论
	 * 作者： 张叶
	 * 创建时间：2018年7月23日  上午11:29:12
	 */
	@ResponseBody
	@RequestMapping(value = "/getComment", method = RequestMethod.POST)
	public ApiResult getComment(HttpServletRequest request,Long easyArId) {
		ApiResult result = new ApiResult();
		EasyAr ea = easyArService.selectByPrimaryKey(easyArId);
		if(ea != null && !"".equals(ea)){
			List<ArComment> list = new ArrayList<ArComment>();
			String where = " and easy_ar_id = " + ea.getId();
			where += " order by created_at";
			Admin admin = safeSessionService.getCurrent(request);
			if(admin != null && !"".equals(admin)){
				list = arCommentService.selectByWhere(where,admin.getId());
			}else{
				list = arCommentService.selectByWhere(where);	
			}
			if(list.size() > 0){
		        for (ArComment arComment : list) {
		        	arComment.setChildren(list.stream().filter(arComment1 -> arComment1.getParentId() != null && arComment1.getParentId().equals(arComment.getId())).collect(Collectors.toList()));
		        }
		        list = list.stream().filter(arComment -> arComment.getDepth() == 1).collect(Collectors.toList());
			}
	        result.data.put("commentList",list);
	        result.data.put("listCount",list.size());
			result.msg.status = 0;
			result.msg.desc = "获取成功";
		}else{
			result.msg.status = 1;
			result.msg.desc = "暂无数据";
		}
		return result;
	}	
}
